package mark.tom.sail;

import androidx.annotation.NonNull;

import java.util.Arrays;
import java.util.List;

import static mark.tom.sail.Util.log_d;
import static mark.tom.sail.Util.log_i;

class DefaultAnalyticsReporter extends AnalyticsReporter {

    // 日志数据网络上报器
    private final RecordUploader mLogReportUploader;

    DefaultAnalyticsReporter(String mIdKey,
                             DataConverter mLogDataConvert,
                             FileGenerator mLogFileGenerator,
                             FileStorage mLogFileStorage,
                             DataSecurity mLogDataSecurity,
                             LogInterceptor logInterceptor,
                             RecordUploader recordUploader) {
        super(mIdKey, mLogDataConvert, mLogFileGenerator, mLogFileStorage, mLogDataSecurity, logInterceptor);
        this.mLogReportUploader = recordUploader;
    }

    @Override
    void reportLog(@NonNull String... tags) {
        // 检查各项控制器
        if (!checkConfiguration()) {
            throw new IllegalArgumentException();
        }
        // 检查是否设置了上报器
        if (mLogReportUploader == null) {
            return;
        }
        log_i(mLogInterceptor, "=====================Analytics Report Begin============================");
        // 获取缓存文件列表
        List<String> fileNamesInCacheDir = mLogFileStorage.getLogFileNamesInCacheDir();
        if (fileNamesInCacheDir == null || fileNamesInCacheDir.size() == 0) {
            log_d(mLogInterceptor, "No Files Need To Upload");
            log_i(mLogInterceptor, "=====================Analytics Report End============================");
            return;
        }
        log_d(mLogInterceptor, "File In Cache Dir Is : " + fileNamesInCacheDir.toString());
        // 是否需要检查tag
        List<String> tagList = Arrays.asList(tags);
        log_d(mLogInterceptor, "Need Upload File Tag Is : " + tagList.toString());
        // 过滤掉不需要上传的文件
        for (int i = fileNamesInCacheDir.size() - 1; i >= 0; i--) {
            String itemTag = mLogFileGenerator.tag(fileNamesInCacheDir.get(i));
            if (!tagList.contains(itemTag)) {
                continue;
            }
            String itemKey = mLogFileGenerator.key(fileNamesInCacheDir.get(i));
            if (!mLogFileGenerator.compare(mIdKey, itemKey)) {
                continue;
            }
            uploadReport(itemTag, fileNamesInCacheDir.get(i));
        }
        log_i(mLogInterceptor, "=====================Analytics Report End============================");
    }

    private void uploadReport(String tag, final String fileName) {
        List<String> fileContent = mLogFileStorage.getLogContent(fileName);
        if (null == fileContent) {
            return;
        }
        log_d(mLogInterceptor, "Need Upload File Name Is : " + fileName + " And Content is + " + fileContent.toString());
        List<String> fileContents = mLogDataSecurity.decrypt(fileContent);
        if (null == fileContents) {
            return;
        }
        log_d(mLogInterceptor, "Decrypted Content is + " + fileContents.toString());
        if (fileContents.size() <= 0) {
            return;
        }
        log_d(mLogInterceptor, "Now Take Uploader ");
        mLogReportUploader.upload(tag, fileContents, new RecordUploader.UploadResult() {
            @Override
            public void success() {
                // 删除日志文件
                mLogFileStorage.deleteLogFile(fileName);
            }
        });
    }

}
